package com.ccn.crm.util;

import com.alibaba.fastjson.JSON;
import com.ccn.crm.interfaces.impl.PushDataImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 从crm获取新建项目
 */
public class GetProjectInfo {
    private static final Logger LOG = LoggerFactory.getLogger(GetProjectInfo.class);
    public static Map<String, Object> getProjectInfo(Map<String, Object> token) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        /**
         *     "corpAccessToken": "AD239FC36625EB189A1ADC211AE74431",
         *     "corpId": "FSCID_A6D390D2E5FFF878491C1F5E6DA2994B",
         *     "currentOpenUserId": "FSUID_48FB359BC8A03783738745CCC02FDE1E",
         */
        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
            //设置查询条件
            Map<Object,Object> data=new HashMap<Object,Object>();
            data.put("dataObjectApiName","object_h249C__c");
                Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
                searchQueryInfo.put("offset",0);
                searchQueryInfo.put("limit",100);
                    //设置过滤添加数组类型
                    List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
                        Map<Object,Object> conditions=new HashMap<Object,Object>();
                            conditions.put("field_name","create_time");
                                List<Object>  values=new ArrayList<>();
                                    long time = new Date().getTime()- 60000*60*2;
//                                    long times= 1535135348167l;
//        System.out.println("times"+times);
                                    Date date = new Date(time);
                                LOG.info("获取项目的时间："+date);
                                    LOG.info("获取项目的时间戳："+time);
                                values.add(time);
                            conditions.put("field_values",values);
                            conditions.put("operator","GTE");
//                        Map<Object,Object> lifeStutes=new HashMap<Object,Object>();
//                        lifeStutes.put("field_name","life_status");
//                        lifeStutes.put("field_values","normal");
//                        lifeStutes.put("operator","EQ");
                    filters.add(conditions);
//                    filters.add(lifeStutes);

        searchQueryInfo.put("filters",filters);


            data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }

    public static Map<String, Object> getProjectInfoById(String objectDataId ,Map<String, Object> token) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/get";
        Map<String, Object> jsonObject = new HashMap<String, Object>();

        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_h249C__c");
        data.put("objectDataId",objectDataId);
        jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }
    public static Map<String, Object> GetProjectUnfinished(Map<String, Object> token) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        /**
         *     "corpAccessToken": "AD239FC36625EB189A1ADC211AE74431",
         *     "corpId": "FSCID_A6D390D2E5FFF878491C1F5E6DA2994B",
         *     "currentOpenUserId": "FSUID_48FB359BC8A03783738745CCC02FDE1E",
         */
        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_h249C__c");
        Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
        searchQueryInfo.put("offset",0);
        searchQueryInfo.put("limit",100);
        //设置过滤添加数组类型
        List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
        Map<Object,Object> conditions=new HashMap<Object,Object>();
            conditions.put("field_name","field_AYAPc__c");
            List<Object>  values=new ArrayList<>();
            values.add("pdZN7S7gN");
            conditions.put("field_values",values);
            conditions.put("operator","ISN");
        Map<Object,Object> cond=new HashMap<Object,Object>();
            cond.put("field_name","field_AYAPc__c");
            List<Object>  val=new ArrayList<>();
            val.add("apJB66nrc");
            cond.put("field_values",val);
            cond.put("operator","ISN");
        Map<Object,Object> conds=new HashMap<Object,Object>();
        conds.put("field_name","field_AYAPc__c");
        List<Object>  vals=new ArrayList<>();
            vals.add("other");
            conds.put("field_values",val);
            conds.put("operator","ISN");
        filters.add(conditions);
        filters.add(cond);
        filters.add(conds);
        searchQueryInfo.put("filters",filters);


        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }
    public static Map<String, Object> GetNoCrmPidByName(Map<String, Object> token,String name) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        /**
         *     "corpAccessToken": "AD239FC36625EB189A1ADC211AE74431",
         *     "corpId": "FSCID_A6D390D2E5FFF878491C1F5E6DA2994B",
         *     "currentOpenUserId": "FSUID_48FB359BC8A03783738745CCC02FDE1E",
         */
        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_h249C__c");
        Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
        searchQueryInfo.put("offset",0);
        searchQueryInfo.put("limit",100);
        //设置过滤添加数组类型
        List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
        Map<Object,Object> conditions=new HashMap<Object,Object>();
        conditions.put("field_name","name");
        List<Object>  values=new ArrayList<>();
        values.add(name);
        conditions.put("field_values",values);
        conditions.put("operator","EQ");
//        Map<Object,Object> cond=new HashMap<Object,Object>();
//        cond.put("field_name","field_AYAPc__c");
//        List<Object>  val=new ArrayList<>();
//        val.add("apJB66nrc");
//        cond.put("field_values",val);
//        cond.put("operator","ISN");
//        Map<Object,Object> conds=new HashMap<Object,Object>();
//        conds.put("field_name","field_AYAPc__c");
//        List<Object>  vals=new ArrayList<>();
//        vals.add("other");
//        conds.put("field_values",val);
//        conds.put("operator","ISN");
        filters.add(conditions);
//        filters.add(cond);
//        filters.add(conds);
        searchQueryInfo.put("filters",filters);


        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }
}
